// 引入 mongo 模型
const gameRepo = require('../model/game')

async function modelSaveOrUpdate(gameObject) {
  if (gameObject) {
    var model = await gameRepo.getExists(gameObject.chsName)
    var result
    if (model === null) {
      result = await gameRepo.modelSave(gameObject)
    } else {
      gameObject._id = model._id // 设置当前文档的主键
      result = await gameRepo.modelUpdate(gameObject)
    }
  } else {
    result = { success: false, message: '缺少数据对象模型' }
  }
  return result
}

async function getModel(chsName) {
  if (chsName) {
    var result = await gameRepo.getExists(chsName)
    return result
  } else {
    result = { success: false, message: '缺少数据对象模型' }
  }
}

module.exports = {
  getModel,
  modelSaveOrUpdate: modelSaveOrUpdate
}
